package com.g8.pethospitallearnbackend.controller;

import com.g8.pethospitallearnbackend.exception.CustomException;
import com.g8.pethospitallearnbackend.interceptors.AuthRequird;
import com.g8.pethospitallearnbackend.service.FileUploadService;
import com.g8.pethospitallearnbackend.utils.responseUtil.ResponseStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


@RestController
@RequestMapping("")
public class FileUploadController {
    @Autowired
    private FileUploadService fileUploadService;

    /**
     * 上传图片/视频，返回url的列表
     * @param files
     * @return
     */
    @PostMapping("/media")
    public String uploadImageAndVideo(@RequestParam("files") List<MultipartFile> files){
        List<String> fileUrls=fileUploadService.uploadImageAndVideo(files);
        if(fileUrls==null||fileUrls.isEmpty()) throw new CustomException(ResponseStatusEnum.BAD_REQUEST,"上传失败");
        //转成字符串
        StringBuffer urls=new StringBuffer();
        boolean first=true;
        for(String fileUrl:fileUrls){
            if(first){
                urls.append(fileUrl);
                first=false;
            }
            else{
                urls.append("|"+fileUrl);
            }
        }
        return urls.toString();
    }
}
